﻿'******************************************************
'******************************************************
'Application Title: Google Widget Editor
'version: 1.0
'Date: 01-02-2008
'Requirements: win2k or later
'Author: Andrew Pye
'Author Email: StretchMasterP@googlemail.com
'License: Released under GNU GENERAL PUBLIC LICENSE v3
'ToDo:
'Changes:Imports System.Text
'******************************************************
'******************************************************

Imports System.Text
Imports System.ComponentModel
''' <summary>
''' Class to handle the user prefs
''' </summary>
''' <remarks></remarks>
Public Class UserPref
    Implements IObjectName

#Region "Enum"
    Public Enum EnumDataType
        list
        [enum]
        [string]
    End Enum
#End Region
#Region "Fields"
    Private _name As String = String.Empty
    Private _display_name As String = String.Empty
    Private _datatype As String = String.Empty
    Private _default_value As String = String.Empty
    Private _enums As Enums_Collection
    Private _Parent As UserPrefs = Nothing
#End Region
#Region "Constructure"
    Public Sub New(ByVal Name As String, ByVal Display_name As String, ByVal Default_value As String, ByVal Datatype As UserPref.EnumDataType)
        With Me
            .Name = Name
            .Display_name = Display_name
            .Default_value = Default_value
            .Datatype = Datatype
        End With
        Me.Enums = New Enums_Collection
        Me.Enums.ParentPref = Me.Name
    End Sub
    Public Sub New()
        Me.Enums = New Enums_Collection
    End Sub
#End Region
#Region "Propertys"
    Protected ReadOnly Property Parent() As Object Implements IObjectName.Parent
        Get
            Return _Parent
        End Get
    End Property
    Public Sub SetParent(ByVal ParentObject As Object) Implements IObjectName.SetParent
        _Parent = ParentObject
    End Sub
    <TypeConverter(GetType(DataTypes)), CategoryAttribute("Misc"), DescriptionAttribute("Type of data")> _
    Public Property Datatype() As String
        Get
            Return _datatype
        End Get
        Set(ByVal value As String)
            _datatype = value
            Me.SetDirty()
        End Set
    End Property
    <CategoryAttribute("Misc"), DescriptionAttribute("Default Value")> _
    Public Property Default_value() As String
        Get
            Return _default_value
        End Get
        Set(ByVal value As String)
            _default_value = value
            Me.SetDirty()
        End Set
    End Property
    <CategoryAttribute("Misc"), DescriptionAttribute("The Name to display")> _
    Public Property Display_name() As String
        Get
            Return _display_name
        End Get
        Set(ByVal value As String)
            _display_name = value
            Me.SetDirty()
        End Set
    End Property
    <BrowsableAttribute(False)> _
    Public Property Enums() As Enums_Collection
        Get
            Return _enums
        End Get
        Set(ByVal value As Enums_Collection)
            _enums = value
            Me.SetDirty()
        End Set
    End Property
    <CategoryAttribute("Misc"), DescriptionAttribute("Name")> _
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
            Me.SetDirty()
            If Me.Enums IsNot Nothing Then Me.Enums.ParentPref = Me.Name
        End Set
    End Property
    <Browsable(False)> _
    Public ReadOnly Property ObjectName() As String Implements IObjectName.ObjectName
        Get
            Return Me.Name
        End Get
    End Property
#End Region
#Region "Methods"
    Public Sub SetDirty()
        If Me.Parent IsNot Nothing Then Me.Parent.SetDirty()
    End Sub
    Public Function BuildXml() As String
        Dim sb As StringBuilder = New StringBuilder
        If Me.Enums.Count = 0 Then
            sb.AppendLine("<UserPref name='" & Me.Name & "' display_name='" & Me.Display_name & "' datatype='" & Me.Datatype.ToString & "' default_value='" & Me.Default_value & "'/>")
        Else
            sb.AppendLine("<UserPref name='" & Me.Name & "' display_name='" & Me.Display_name & "' datatype='" & Me.Datatype.ToString & "' default_value='" & Me.Default_value & "'>")
            For Each Tobj As EnumValue In _enums
                sb.AppendLine("<EnumValue Display_value='" & Tobj.Display_value & "' value='" & Tobj.Value & "'/>")
            Next
            sb.AppendLine("</UserPref>")
        End If

        Return sb.ToString
    End Function
#End Region
#Region "Constructor"
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        Me.Enums = Nothing
    End Sub
#End Region

    
End Class
''' <summary>
''' Collection of user prefs
''' </summary>
''' <remarks></remarks>
Public Class UserPrefs
    Inherits ObjectModel.Collection(Of UserPref)
    Implements IObjectName
#Region "Fields"
    Private _Parent As ModulePrefs = Nothing
    Private _IsDirty As Boolean = False
#End Region

#Region "Methods"
    Public Sub SetDirty()
        If Me.Parent IsNot Nothing Then Me.Parent.SetDirty()
    End Sub
    Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As UserPref)
        MyBase.InsertItem(index, item)
        item.SetParent(Me)
    End Sub
    Public Shadows Function Add(ByVal Name As String, ByVal Display_name As String, ByVal Default_value As String, ByVal Datatype As UserPref.EnumDataType) As UserPref
        Dim o_UserPref As New UserPref()
        With o_UserPref
            .Name = Name
            .Display_name = Display_name
            .Default_value = Default_value
            .Datatype = Datatype
        End With
        Return Me.Add(o_UserPref)
    End Function
    Public Shadows Function Add(ByVal oUserPref As UserPref) As UserPref
        MyBase.Add(oUserPref)
        Return oUserPref
    End Function

    Public Function BuildXml() As String
        Dim sb As StringBuilder = New StringBuilder
        For Each Tobj As UserPref In Me
            Dim strbuf As String = Tobj.BuildXml
            If Not String.IsNullOrEmpty(strbuf) Then
                sb.AppendLine(Tobj.BuildXml)
            End If
        Next
        Return sb.ToString
    End Function
#End Region
#Region "Properties"
    Public Property IsDirty() As Boolean
        Get
            Return _IsDirty
        End Get
        Set(ByVal value As Boolean)
            _IsDirty = value

        End Set
    End Property
    Protected ReadOnly Property Parent() As Object Implements IObjectName.Parent
        Get
            Return _Parent
        End Get
    End Property
    Public Sub SetParent(ByVal ParentObject As Object) Implements IObjectName.SetParent
        _Parent = ParentObject
    End Sub
    Public ReadOnly Property ObjectName() As String Implements IObjectName.ObjectName
        Get
            Return "UserPrefs"
        End Get
    End Property

#End Region
End Class